<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    :cancelText="$t('关闭')"
  >
    <template slot="footer">
      <a-row :gutter="20">
        <a-col :span="24">
          <span style="float: left; font-weight: bolder"> </span>
          <a-button type="primary" v-if="state == 0" @click="handleOk(0)" v-has="'BisGoodsInventoryController:edit'">{{ $t('暂存') }}</a-button>
          <a-button type="primary" v-if="state != 1" @click="handleSubmit" v-has="'BisGoodsInventoryController:edit'">{{ $t('盘点') }}</a-button>
          <a-button @click="handleCancel">{{ $t('取消') }}</a-button>
        </a-col>
      </a-row>
    </template>
    <div>
      <a-row style="margin-bottom: 20px" v-if="dataForm.id || state == 1 || state == 2">
        <a-col :span="24">
          <a-button v-if="dataForm.id" type="primary" @click="handleExportXls($t('盘点单明细'))" v-has="'BisGoodsInventoryController:export'">{{
            $t('导出')
          }}</a-button>
          <a-button v-if="state == 1 || state == 2" type="primary" @click="printReport" style="margin-left: 8px" v-has="'BisGoodsInventoryController:print'">{{
            $t('打印')
          }}</a-button>
        </a-col>
      </a-row>
      <div>
        <a-row :gutter="10">
          <a-col :span="5" style="display: flex; align-items: center">
            <span style="white-space: nowrap; padding-right: 4px">{{ $t('仓库') }}:</span>
            <a-tree-select
              v-if="state != 1 && state != 2"
              allowClear
              v-model="dataForm.store"
              :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
              :tree-data="storeData"
              :placeholder="$t('请选择')"
              @change="(val) => selectStore(val, true)"
              style="width: 100%"
            />
            <span v-if="state == 1 || state == 2">
              {{ dataForm.store_dictText }}
            </span>
          </a-col>
          <a-col :span="6" style="display: flex; align-items: center" v-if="state != 1 && state != 2">
            <span style="white-space: nowrap; padding-right: 4px">{{ $t('商品') }}:</span>
            <a-auto-complete
              :disabled="!dataForm.store"
              option-label-prop="label"
              ref="goodsId"
              v-model="keyword"
              style="width: 100%"
              :placeholder="$t('请输入商品编码/名称/条码')"
              :filter-option="false"
              :not-found-content="null"
              :dropdown-match-select-width="false"
              :dropdown-style="{ width: '420px' }"
              @select="handleGoodsChange"
            >
              <template #dataSource>
                <a-select-opt-group v-for="group in goodsData" :key="group.title">
                  <template #label>{{ $t('商品信息') }}</template>
                  <a-select-option
                    v-for="goods in group.addGoodsList"
                    :key="goods.id"
                    :value="JSON.stringify(goods)"
                  >
                    {{
                      goods.stock
                        ? (goods.goodsCode
                            ? goods.goodsCode + '(' + goods.goodsId_dictText + ')'
                            : goods.goodsId_dictText) +
                          '  [ ' +
                          goods.stock +
                          ' ]'
                        : goods.goodsCode
                        ? goods.goodsCode + '(' + goods.goodsId_dictText + ')'
                        : goods.goodsId_dictText
                    }}
                  </a-select-option>
                </a-select-opt-group>
              </template>
              <a-input-search @search="handleGoodsSearch" enter-button></a-input-search>
            </a-auto-complete>
          </a-col>
          <a-col :span="4" v-if="state != 1 && state != 2" style="display: flex; align-items: center">
            <span style="white-space: nowrap; padding-right: 4px">{{ $t('实盘数量') }}:</span>
            <a-input-number :formatter="formatter" :parser="parser"
              ref="number"
              v-model="curGoods.number"
              style="width: 100%"
              @pressEnter="addGoods"
            />
          </a-col>
          <a-col :span="2" v-if="state != 1 && state != 2">
            <a-button type="primary" @click="addGoods">{{ $t('添加') }}</a-button>
          </a-col>
        </a-row>
        <div v-if="state != 1 && state != 2" style="margin: 10px; font-size: 14px; font-weight: bold">
          {{ $t('商品列表') }}:
          <span style="margin-left: 20px" v-if="state != 1 && state != 2">
            {{ $t('商品分类') }}:
            <a-tree-select
              allowClear
              v-model="dataForm.goodsId"
              :disabled="!dataForm.store"
              :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
              :tree-data="treeData"
              :placeholder="$t('请选择')"
              style="width: 200px"
            />
            <a-button style="margin-left: 10px" type="primary" @click="searchHandle" icon="search">{{ $t('查询') }}</a-button>
          </span>
        </div>
        <a-table
          :columns="columns"
          :dataSource="dataSource"
          size="middle"
          bordered
          class="j-table-force-nowrap"
          :pagination="false"
          style="padding-left: 11px"
          :customRow="customRow"
        >
          <template slot="action" slot-scope="text, record">
            <a-icon type="close" @click="deleteGoods(text, record)" />
          </template>
          <template slot="unitPrice" slot-scope="text">
            <span>{{isPriceDecimal?text.toFixed(2):text}}</span>
          </template>
          <template slot="systemAmount" slot-scope="text">
            <span>{{isPriceDecimal?text.toFixed(2):text.toFixed(0)}}</span>
          </template>
          <template slot="actualAmount" slot-scope="text">
            <span>{{isPriceDecimal?(text?text.toFixed(2):0):text.toFixed(0)}}</span>
          </template>
          <template slot="errorAmount" slot-scope="text">
            <span>{{isPriceDecimal?text.toFixed(2):text.toFixed(0)}}</span>
          </template>
          <template slot="actualStock" slot-scope="text, record, index">
            <a-input-number :formatter="formatter" :parser="parser" v-model="record.actualStock" @change="changActualStock(record, index)"></a-input-number>
          </template>
          <template slot="comment" slot-scope="text, record">
            <a-input v-model="record.comment"></a-input>
          </template>
        </a-table>
        <div style="margin-top: 20px; font-weight: bolder">
          {{ $t('备注') }}：
          <a-input
            v-if="state != 1 && state != 2"
            style="width: 300px"
            v-model="dataForm.comment"
            :placeholder="$t('请输入备注')"
          />
          <span v-if="state == 1 || state == 2">{{ dataForm.comment }}</span>
        </div>
      </div>
      <goods-inventory-submit ref="modelForm" @ok="submitCallback"></goods-inventory-submit>
      <goods-print-modal ref="printModal"></goods-print-modal>
    </div>
  </j-modal>
</template>

<script>
import Vue from 'vue'
import { TENANT_ID } from '@/store/mutation-types'
import { httpAction, getAction, downFile } from '@/api/manage'
import { formatter, parser } from '@/api/comm'
import GoodsInventorySubmit from './GoodsInventorySubmit'
import GoodsPrintModal from './GoodsPrintModal'
let that, selectedIds
export default {
  name: 'GoodsInventoryStandbyModal',
  components: {
    GoodsInventorySubmit,
    GoodsPrintModal,
  },
  data() {
    return {
      formatter,
      parser,
      title: '',
      width: 1250,
      visible: false,
      disableSubmit: false,
      isPriceDecimal: true,
      dataForm: {
        store: '',
      },
      treeData: [],
      columns: [],
      columns1: [
        {
          title: '',
          dataIndex: 'action',
          align: 'center',
          width: 40,
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
                color: '#1890ff',
                fontSize: '25px',
                height: '32px',
                lineHeight: '32px',
                fontWeight: 'bold',
                cursor: 'pointer',
              },
            }
          },
          customCell: function (column) {
            return {
              style: {
                textAlign: 'center',
                color: '#1890ff',
                fontSize: '16px',
                height: '32px',
                lineHeight: '32px',
                fontWeight: 'bold',
                cursor: 'pointer',
              },
            }
          },
          scopedSlots: { customRender: 'action' },
        },
        {
          title: this.$t('序号'),
          dataIndex: '',
          key: 'rowIndex',
          align: 'center',
          width: 36,
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: this.$t('商品编码'),
          dataIndex: 'goodsCode',
          align: 'center',
          width: 90,
        },
        {
          title: this.$t('商品名称'),
          dataIndex: 'goodsId_dictText',
          align: 'center',
          width: 250,
        },
        {
          title: this.$t('成本'),
          key: 'unitPrice',
          dataIndex: 'unitPrice',
          align: 'center',
          width: 80,
          customRender: (t, r) => {
            if (!t && t !== 0) {
              return '0.00'
            }
            return t
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('系统数量'),
          key: 'systemStock',
          dataIndex: 'systemStock',
          width: 80,
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'systemStock' },
        },
        {
          title: this.$t('系统金额'),
          key: 'systemAmount',
          dataIndex: 'systemAmount',
          align: 'center',
          width: 90,
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'systemAmount' },
        },
        {
          title: this.$t('实盘数量'),
          key: 'actualStock',
          dataIndex: 'actualStock',
          width: 80,
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'actualStock' },
        },
        {
          title: this.$t('实盘金额'),
          dataIndex: 'actualAmount',
          align: 'center',
          width: 90,
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'actualAmount' },
        },
        {
          title: this.$t('差异'),
          key: 'error',
          dataIndex: 'error',
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: function (text) {
            if(text || text === 0){
              return text.toFixed(0)
            }
            return ''
          },
        },
        {
          title: this.$t('差异金额'),
          key: 'errorAmount',
          dataIndex: 'errorAmount',
          align: 'center',
          width: 90,
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'errorAmount' },
        },
        {
          title: this.$t('备注'),
          key: 'comment',
          dataIndex: 'comment',
          align: 'center',
          width: 100,
          scopedSlots: { customRender: 'comment' },
        },
      ],
      columns2: [
        {
          title: this.$t('序号'),
          dataIndex: '',
          key: 'rowIndex',
          align: 'center',
          width: 36,
          customRender: function (t, r, index) {
            if (r.goodsId) {
              return parseInt(index) + 1
            }
            return ''
          },
        },
        {
          title: this.$t('商品编码'),
          dataIndex: 'goodsCode',
          align: 'center',
          width: 100,
        },
        {
          title: this.$t('商品名称'),
          dataIndex: 'goodsId_dictText',
          align: 'center',
          width: 250,
        },
        {
          title: this.$t('成本'),
          key: 'unitPrice',
          dataIndex: 'unitPrice',
          align: 'center',
          width: 80,
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'unitPrice' },
        },
        {
          title: this.$t('系统数量'),
          key: 'systemStock',
          dataIndex: 'systemStock',
          width: 80,
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('系统金额'),
          key: 'systemAmount',
          dataIndex: 'systemAmount',
          align: 'center',
          width: 90,
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'systemAmount' },
        },
        {
          title: this.$t('实盘数量'),
          key: 'actualStock',
          dataIndex: 'actualStock',
          width: 80,
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('实盘金额'),
          dataIndex: 'actualAmount',
          align: 'center',
          width: 90,
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'actualAmount' },
        },
        {
          title: this.$t('差异'),
          key: 'error',
          dataIndex: 'error',
          customHeaderCell: function (column) {
            return {
              style: {
                textAlign: 'center',
              },
            }
          },
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: function (text) {
            return text.toFixed(0)
          },
        },
        {
          title: this.$t('差异金额'),
          key: 'errorAmount',
          dataIndex: 'errorAmount',
          align: 'center',
          width: 90,
          customCell: function (record, rowIndex) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          scopedSlots: { customRender: 'errorAmount' },
        },
        {
          title: this.$t('备注'),
          key: 'comment',
          dataIndex: 'comment',
          align: 'center',
          width: 100,
        },
      ],
      url: {
        add: '/goods/bisGoodsInventory/add',
        edit: '/goods/bisGoodsInventory/edit',
        exportXlsUrl: '/goods/bisGoodsInventory/detailExportXls',
      },
      dataSource: [],
      goodsData: [{ addGoodsList: [] }],
      state: 0,
      curGoods: {},
      keyword: '',
      storeData: [],
      tenantid: Vue.ls.get(TENANT_ID),
    }
  },
  async created() {
    that = this
    that.isPriceDecimal=await that.getRule()
  },
  methods: {
    getRule(){
      return new Promise(resolve=>{
        getAction('/bis/bisSysParameter/list', { name: 'isPriceDecimal' }).then((res) => {
          let value=true
          if (res.success&&res.result.total > 0) {
            let isPriceDecimal = res.result.records[0]
            value = isPriceDecimal.ivalue ? true : false
          }
          console.log(value)
          resolve(value)
        })
      })
    },
    handleGoodsSearch(value) {
      that.getGoodsList(value)
    },
    getGoodsList(value) {
        let querydata = {}
        that.goodsData = []
        querydata.pageNo = 1
        querydata.pageSize = 10
        querydata.goodsInfo = value
        querydata.storeId = that.dataForm.store
        getAction('/goods/bisGoodsStock/list', querydata).then((res) => {
          if (res.success) {
            if (res.result.total > 0) {
              let goodsList = res.result.records
              if (goodsList.length > 1) {
                that.goodsData.push({ addGoodsList: goodsList })
              } else {
                that.handleGoodsChange(JSON.stringify(goodsList[0]))
              }
            } else {
              that.$message.warn(that.$t('未找到符合的商品'))
              that.keyword = ''
            }
          }
        })
    },
    add(obj) {
      that.visible = true
      that.dataSource = []
      that.curGoods = {}
      that.keyword = ''
      selectedIds = []
      let record = {}
      if(obj){
        if(obj.storeId){
          record.store = obj.storeId
        }
        if(obj.selectedIds){
          selectedIds = obj.selectedIds
        }
      }
      that.edit(record)
    },
    edit(record, state) {
      that.curGoods = {}
      that.goodsData = []
      that.goodsData.push({ addGoodsList: [] })
      if (state == undefined) {
        that.state = 0
        that.columns = that.columns1
      } else {
        that.state = state
        that.columns = that.columns2
      }
      if(!record.id&&selectedIds.length>0){
        let dataSourceTwo = []
        getAction('/goods/bisGoodsStock/list', {goodsId: selectedIds.join(','), storeId: record.store, pageSize: 1000}).then((res) => {
          console.log(res)
          if(res.success){
            res.result.records.forEach((col) => {
              let aa = {
                goodsId: col.goodsId,
                goodsCode: col.goodsCode,
                goodsId_dictText: col.goodsId_dictText,
                unitPrice: col.cost,
                totalAmount: '0.00',
                comment: '',
                systemStock: col.stock,
                actualStock: col.stock,
                error: 0,
                systemAmount: col.cost * col.stock,
                actualAmount: col.cost * col.stock,
                errorAmount: 0,
              }
              dataSourceTwo.push(aa)
            })
            dataSourceTwo.forEach((dst) => {
              let x = that.dataSource.findIndex((ds) => {
                return ds.goodsId == dst.goodsId
              })
              if (x == -1) {
                that.dataSource.push(dst)
              }
            })
            that.dataSource.forEach((d, i) => {
              d.sign = i
            })
          }
        })
      }
      that.visible = true
      that.dataForm = Object.assign({}, record)
      that.getStore()
      that.select()
      if (that.dataForm.id) {
        that.getGoodsList()
        that.getOrderDetail()
      }
    },
    getStore() {
      let that = this
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'createTime'
      param.order = 'desc'
      param.tenantId = that.tenantid ? that.tenantid : 0
      getAction('/goods/bisGoodsStore/rootList', param).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getStoreHasChild(col)
              }
            })
            that.storeData = that.getDataByResult(res.result.records)
          } else {
            that.storeData = []
          }
        } else {
          that.$alert(res.message, that.$t('提示'), {
            confirmButtonText: that.$t('确定'),
            callback: (action) => {},
          })
        }
      })
    },
    getStoreHasChild(record) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = record.id
      query.column = 'createTime'
      query.order = 'desc'
      query.tenantId = that.tenantid ? that.tenantid : 0
      getAction('/goods/bisGoodsStore/rootList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            record.children = that.getDataByResult(res.result.records)
            that.storeData = [...that.storeData]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getOrderDetail() {
      that.dataSource = []
      getAction('/goods/bisGoodsInventory/queryBisGoodsInventoryDetailsByMainId', { id: that.dataForm.id }).then(
        (res) => {
          if (res.success) {
            that.dataSource = res.result
            that.dataSource.forEach((col, i) => {
              col.sign = i
            })
          }
        }
      )
    },
    addGoods() {
      if (that.dataForm.store == '' || that.dataForm.store == undefined) {
        that.$message.warning(that.$t('请选择要盘点的仓库'))
        return
      } else {        
        if(!that.curGoods.goodsId){
          return
        }
        let obj = {
          goodsId: that.curGoods.goodsId,
          goodsCode: that.curGoods.goodsCode,
          goodsId_dictText: that.curGoods.goodsId_dictText,
          unitPrice: that.curGoods.cost,
          systemStock: that.curGoods.stock,
          actualStock: that.curGoods.number,
          error: parseInt(that.curGoods.number) - that.curGoods.stock,
          systemAmount: that.curGoods.cost * that.curGoods.stock,
          actualAmount: that.curGoods.cost * parseInt(that.curGoods.number),
          errorAmount: that.curGoods.cost * (parseInt(that.curGoods.number) - that.curGoods.stock),
          comment: '',
        }
        let x = that.dataSource.findIndex((d) => {
          return d.goodsId == obj.goodsId
        })
        if (x == -1) {
          that.dataSource.unshift(obj)
          that.dataSource.forEach((d, i) => {
            d.sign = i
          })
        } else {
          that.$message.warning(`${obj.goodsCode}${obj.goodsId_dictText}` + that.$t('商品已存在'))
        }
        that.$nextTick(() => {
          that.curGoods = {}
          that.keyword = ''
          that.$refs.goodsId.focus()
        })
      }
    },
    selectStore(val, status) {
      if (status) {
        if (that.dataSource.length > 0) {
          that.$confirm({
            title: that.$t('提示'),
            content: that.$t('切换仓库将清空商品列表，确定要切换吗？'),
            okText: that.$t("确定"),
            cancelText: that.$t("取消"),
            onOk() {
              that.dataSource = []
              that.$nextTick(() => {
                that.$refs.goodsId.focus()
              })
              return
            },
            onCancel() {},
          })
        }
      }
      //that.getGoodsList()
      that.$nextTick(() => {
        that.$refs.goodsId.focus()
      })
    },
    handleGoodsChange(value) {
      that.curGoods = JSON.parse(value)
      that.curGoods.number = that.curGoods.stock
      that.keyword = that.curGoods.goodsId_dictText
      that.$refs.number.focus()
    },
    searchHandle() {
      if (that.dataForm.store == undefined) {
        that.$message.warning(that.$t('请选择要盘点的仓库'))
        return
      }
      let querydata = {}
      querydata.pageNo = 1
      querydata.pageSize = 9999
      querydata.parentKindId = that.dataForm.goodsId
      querydata.storeId = that.dataForm.store
      getAction('/goods/bisGoodsStock/list', querydata).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            let goodsList = res.result.records
            if (goodsList.length > 1) {
              goodsList.forEach((col) => {
                let aa = {
                  goodsId: col.goodsId,
                  goodsId_dictText: col.goodsId_dictText,
                  goodsCode: col.goodsCode,
                  systemStock: col.stock,
                  actualStock: col.stock,
                  error: 0,
                  comment: '',
                  unitPrice: col.cost,
                  actualAmount: col.stock * col.cost,
                  systemAmount: col.stock * col.cost,
                  errorAmount: 0,
                }
                that.dataSource.push(aa)
              })
              that.goodsData.push({ addGoodsList: goodsList })
            } else {
              that.handleGoodsChange(goodsList[0])
            }
          } else {
            that.$message.warn(that.$t('未找到符合的商品'))
            that.keyword = ''
            that.curGoods = {}
            that.$nextTick(() => {
              that.$refs.goodsId.focus()
            })
          }
        }
      })
    },
    deleteGoods(a, b) {
      that.dataSource.forEach((col) => {
        if (col.goodsId == b.goodsId) {
          let index = that.dataSource.indexOf(col)
          that.dataSource.splice(index, 1)
        }
      })
    },
    changActualStock(record, index) {
      if (record.actualStock == '' || isNaN(record.actualStock)) {
        that.$set(that.dataSource[index], 'error', 0 - record.systemStock)
        that.$set(that.dataSource[index], 'actualAmount', 0)
        that.$set(that.dataSource[index], 'errorAmount', 0 - record.systemAmount)
      } else {
        that.$set(that.dataSource[index], 'error', (record.actualStock?parseFloat(record.actualStock):0) - parseFloat(record.systemStock))
        that.$set(that.dataSource[index], 'actualAmount', (record.actualStock?parseFloat(record.actualStock):0) * record.unitPrice)
        let errorAmount = (record.actualStock?parseFloat(record.actualStock):0) * record.unitPrice - record.systemAmount
        that.$set(that.dataSource[index], 'errorAmount', errorAmount)
      }
      that.$forceUpdate()
    },
    changeGoods(record, good) {
      record.goodsId_dictText = good.goodsId_dictText
      record.goodsCode = good.goodsCode
      record.unitPrice = good.cost
      record.systemStock = good.stock
      record.systemAmount = good.cost * good.stock
      record.actualStock = good.stock
      record.actualAmount = good.cost * good.stock
      record.error = 0
      record.errorAmount = 0
    },
    handleOk(state) {
      if (that.dataForm.store == '' || that.dataForm.store == undefined) {
        that.$message.warning(that.$t('请选择要盘点的仓库'))
        return
      }
      if (that.dataSource.length == 0) {
        that.$message.warning(that.$t('商品不能为空'))
        return
      }
      let arr = that.dataSource.filter((d) => {
        return d.number === '' || d.number == null
      })
      if (arr.length > 0) {
        that.$message.warning(that.$t('盘点数量不能为空'))
        return
      }
      let query = {}
      query = that.dataForm
      query.bisGoodsInventoryDetailsList = that.dataSource
      query.state = state
      let method = ''
      let url = ''
      if (that.dataForm.id) {
        method = 'put'
        url = that.url.edit
      } else {
        method = 'post'
        url = that.url.add
      }
      httpAction(url, query, method).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          if (state == 1) {
            that.submitCallback()
          } else {
            that.$emit('ok')
            that.$set(that.dataForm, 'id', res.result.id)
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    handleSubmit() {
      let list = []
      that.dataSource.forEach((col) => {
        if (col.error != 0) {
          list.push(col)
        }
      })
      if (list.length != 0) {
        that.$refs.modelForm.edit(list, that.dataForm)
      } else {
        that.$confirm({
          title: that.$t('提示'),
          content: that.$t('盘点操作将按本次的实盘数量更新系统库存，您确定要执行盘点吗？'),
          okText: that.$t("确定"),
          cancelText: that.$t("取消"),
          onOk() {
            that.handleOk(1)
          },
        })
      }
    },
    select() {
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'sort'
      param.order = 'asc'
      param.tenantId = that.tenantid ? that.tenantid : 0
      getAction('/goods/bisGoodsKind/rootList', param).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            that.treeData = that.getDataByResult(res.result.records)
          } else {
            that.treeData = []
          }
        } else {
          that.$alert(res.message, that.$t('提示'), {
            confirmButtonText: that.$t('确定'),
            callback: (action) => {},
          })
        }
      })
    },
    getHasChild(record) {
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.form = '1,7'
      query.pid = record.id
      query.tenantId = that.tenantid ? that.tenantid : 0
      getAction('/goods/bisGoodsKind/rootList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            record.children = that.getDataByResult(res.result.records)
            that.treeData = [...that.treeData]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      return result.map((item) => {
        return item
      })
    },
    close() {
      that.$emit('close')
      that.visible = false
    },
    submitCallback() {
      that.$emit('ok')
      that.visible = false
    },
    handleCancel() {
      that.close()
    },
    handleExportXls(fileName) {
      if (!fileName || typeof fileName != 'string') {
        fileName = that.$t('导出文件')
      }
      let param = {}
      param.orderId = that.dataForm.id
      downFile(that.url.exportXlsUrl, param).then((data) => {
        if (!data) {
          that.$message.warning(that.$t('文件下载失败'))
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    },
    printReport() {
      that.$refs.printModal.show(that.dataForm, 4)
    },
    customRow(r, i) {
      return {
        style: {
          fontWeight: r.goodsId ? 400 : 700,
        },
      }
    },
  },
}
</script>